package com.jsmile.cloud.msgcenter.zf.strategy.pay.wx;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.bean.RefundResult;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.jsmile.cloud.accountcenter.api.enums.PayTypeEnum;
import com.jsmile.cloud.msgcenter.pay.model.PayOrderInfo;
import com.jsmile.cloud.msgcenter.pay.model.RefundOrderInfo;
import com.jsmile.cloud.msgcenter.web.config.PayConfig;
import com.jsmile.cloud.msgcenter.zf.strategy.interfaces.IPay;
import com.jsmile.mall.api.kit.number.BigDecimalKit;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class WxJsFzPayStrategy implements IPay {

    @Override
    public Map pay(List<PayOrderInfo> payOrders) {
        Long payAmount = payOrders.stream().mapToLong(PayOrderInfo::getPayAmount).sum();
        PayOrder order = new PayOrder(payOrders.get(0).getPayTitle(), payOrders.get(0).getPayDetial(), BigDecimalKit.of(payAmount).divide("100").get(),
            payOrders.get(0).getOutTradeNo(), WxTransactionType.JSAPI);
        order.setExpirationTime(DateUtils.addMinutes(new Date(), 30));// 30分钟后订单过期
        order.setOpenid(payOrders.get(0).getOpenId());
        order.addAttr("profit_sharing", "Y");
        order.addAttr("appid", "服务商商户的APPID");
        order.addAttr("sub_mch_id", "子商户号");
        Map orderInfo = PayConfig.getPayService(payOrders.get(0).getPayAppId(), payOrders.get(0).getPayType()).orderInfo(order);
        orderInfo.put("code", 0);
        return orderInfo;
    }

    @Override
    public PayTypeEnum payType() {
        return PayTypeEnum.WX_FZ;
    }

    @Override
    public RefundResult refund(RefundOrderInfo refundOrderInfo) {
        RefundOrder order = new RefundOrder("", "20122701502300700000310007553690", new BigDecimal(300), new BigDecimal(300));
        order.setRefundNo("21378554545665");
        RefundResult orderInfo = PayConfig.getPayService("wx880d006f00ee05b4", 5).refund(order);
        return orderInfo;
    }
}
